<template>
  <div>
    <el-card>
      <CategorySelector
        :isShowList="isShowList"
        @changeCategory="changeCategory"
      ></CategorySelector>
    </el-card>

    <el-card style="margin: 20px 0">
      <div v-show="!isShowSpuForm && !isShowSkuForm">
        <!-- SUP分页列表 -->
        <el-button
          type="primary"
          icon="el-icon-plus"
          @click="showAddSpuForm"
          :disabled="!category3Id"
          >添加SPU</el-button
        >

        <el-table border style="width: 100%" :data="spuList">
          <el-table-column type="index" label="序号" width="80" align="center">
          </el-table-column>
          <el-table-column prop="spuName" label="SPU名称" width="width">
          </el-table-column>
          <el-table-column prop="description" label="SPU描述" width="width">
          </el-table-column>
          <el-table-column prop="prop" label="操作" width="width">
            <template slot-scope="{ row, $index }">
              <HintButton
                type="success"
                title="添加sku"
                icon="el-icon-plus"
                size="mini"
                @click="showAddSkuForm(row)"
              ></HintButton>
              <HintButton
                type="warning"
                icon="el-icon-edit"
                size="mini"
                title="修改spu"
                @click="showUpdateSpuForm(row)"
              ></HintButton>
              <HintButton
                type="info"
                icon="el-icon-info"
                size="mini"
                title="查看spu的sku列表"
                @click="isShowSkuList(row)"
              ></HintButton>
              <el-popconfirm
                :title="`确定删除${row.spuName}吗？`"
                @onConfirm="deleteSpu(row)"
              >
                <HintButton
                  slot="reference"
                  type="danger"
                  icon="el-icon-delete"
                  size="mini"
                  title="删除spu"
                ></HintButton>
              </el-popconfirm>
            </template>
          </el-table-column>
        </el-table>

        <!-- 分页器 -->
        <!-- @size-change="handleSizeChange"
       @current-change="handleCurrentChange" -->
        <el-pagination
          background
          style="text-align: center"
          :current-page="page"
          :page-sizes="[2, 5, 7]"
          :page-size="limit"
          :pager-count="5"
          layout="prev, pager, next, jumper,->,sizes,total"
          :total="total"
          @current-change="getSpuList"
          @size-change="handleSizeChange"
        >
        </el-pagination>
      </div>

      <!-- SpuForm页面 -->
      <SpuForm
        v-show="isShowSpuForm"
        ref="spu"
        :isShowSpuForm.sync="isShowSpuForm"
        @successBack="successBack"
      ></SpuForm>

      <!-- SkuForm页面 -->
      <SkuForm
        v-show="isShowSkuForm"
        ref="sku"
        :isShowSkuForm.sync="isShowSkuForm"
      ></SkuForm>
    </el-card>

    <el-dialog
      :title="`${spu.spuName}的sku列表`"
      :visible.sync="dialogTableVisible"
      :before-close="closeDialog"
    >
      <el-table :data="skuList" v-loading="loading">
        <el-table-column
          property="skuName"
          label="名称"
          width="width"
        ></el-table-column>
        <el-table-column
          property="price"
          label="价格"
          width="width"
        ></el-table-column>
        <el-table-column
          property="weight"
          label="重量"
          width="width"
        ></el-table-column>
        <el-table-column label="默认图片" width="width">
          <template slot-scope="{ row, $index }">
            <img
              :src="row.skuDefaultImg"
              alt=""
              style="width: 100px; height: 100px"
            />
          </template>
        </el-table-column>
      </el-table>
    </el-dialog>
  </div>
</template>

<script>
import SkuForm from "./components/SkuForm.vue";
import SpuForm from "./components/SpuForm.vue";
export default {
  name: "Spu",
  components: {
    SkuForm,
    SpuForm,
  },
  data() {
    return {
      category1Id: "",
      category2Id: "",
      category3Id: "",
      isShowList: true,
      spuList: [],
      page: 1,
      limit: 2,
      total: 0,

      //下面2个数据是控制三个页面之间的切换
      isShowSpuForm: false,
      isShowSkuForm: false,

      dialogTableVisible: false,

      spu: {},
      skuList: [],
      loading: false,
    };
  },
  methods: {
    changeCategory({ categoryId, level }) {
      if (level === 1) {
        this.category2Id = "";
        this.category3Id = "";
        this.spuList = [];
        this.category1Id = categoryId;
      } else if (level === 2) {
        this.category3Id = "";
        this.spuList = [];
        this.category2Id = categoryId;
      } else {
        this.category3Id = categoryId;
        this.getSpuList();
      }
    },
    //获取spu分页列表并切换页面
    async getSpuList(pager = 1) {
      this.page = pager;
      let { page, limit, category3Id } = this;
      try {
        const result = await this.$API.spu.getList(page, limit, category3Id);

        if (result.code === 20000 || result.code === 200) {
          this.spuList = result.data.records;
          this.total = result.data.total;
        } else {
          this.$message.error("获取SUP分页列表失败");
        }
      } catch (error) {
        this.$message.error("请求获取SUP分页列表失败");
      }
    },
    //切换页码数量
    handleSizeChange(limit) {
      this.limit = limit;
      this.getSpuList();
    },
    //点击添加spu按钮
    showAddSpuForm() {
      this.isShowSpuForm = true;
      this.$refs.spu.getInitAddSpuFormData(this.category3Id);
    },
    //点击修改spu按钮
    showUpdateSpuForm(row) {
      this.isShowSpuForm = true;
      this.$refs.spu.getInitUpdateSpuFormData(row, this.category3Id);
    },
    //点击添加sku按钮
    showAddSkuForm(row) {
      this.isShowSkuForm = true;
      this.$refs.sku.getInitAddSkuFormData(
        row,
        this.category1Id,
        this.category2Id
      );
    },
    //保存成功的回调
    successBack(spuId) {
      if (spuId) {
        //修改
        this.getSpuList(this.page);
      } else {
        //添加
        this.getSpuList();
      }
    },
    // 点击删除
    async deleteSpu(row) {
      try {
        const result = await this.$API.spu.remove(row.id);
        if (result.code === 20000 || result.code === 200) {
          this.$message.success("删除spu成功");
          this.getSpuList(this.spuList.length > 1 ? this.page : this.page - 1);
        } else {
          this.$message.error("删除spu失败");
        }
      } catch (error) {
        this.$message.error("请求删除spu失败");
      }
    },
    //查看sku列表
    async isShowSkuList(row) {
      this.spu = row;
      this.dialogTableVisible = true;
      // 清空上一次图片数据
      // this.skuList = [];
      this.loading = true;
      try {
        const result = await this.$API.sku.getListBySpuId(row.id);
        if (result.code === 20000 || result.code === 200) {
          this.$message.success("获取sku列表成功");
          this.skuList = result.data;
        } else {
          this.$message.success("获取sku列表失败");
        }
      } catch (error) {
        this.$message.success("请求获取sku列表失败");
      }
      this.loading = false;
    },
    closeDialog(done) {
      this.skuList = [];
      this.dialogTableVisible = false;
    
    },
  },
  //三级联动的可操作性
  watch: {
    isShowSpuForm: {
      handler(newValue, oldValue) {
        this.isShowList = !newValue;
      },
    },
    isShowSkuForm: {
      handler(newValue, oldValue) {
        this.isShowList = !newValue;
      },
    },
  },
};
</script>

<style scoped>
</style>